package demo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Executable;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Service {

    public static void main(String[] args) {
        Service service = new Service();
        try {
            service.fun3();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void fun1() throws IOException {

        ServerSocket serverSocket = new ServerSocket(8080);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();

        int len;
        byte[] buf = new byte[1024];
        while((len = inputStream.read(buf)) != -1){
            String str = new String(buf,0,len);
            System.out.println(str);
        }

        inputStream.close();
        socket.close();
        serverSocket.close();

    }

    private void fun2() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        while (true){
            Socket socket = serverSocket.accept();
            String ip = socket.getInetAddress().getHostAddress();
            System.out.println("欢迎"+ip+"进入聊天室");
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        try {
                            InputStream inputStream = socket.getInputStream();
                            byte[] buf = new byte[1024];
                            int len ;
                            StringBuffer stringBuffer = new StringBuffer();
                            while ( (len = inputStream.read(buf)) != -1){
                                String s = new String(buf, 0, len);
                                stringBuffer.append(s);
                                if (s.contains("#")){
                                    stringBuffer.deleteCharAt(stringBuffer.length() - 1);
                                    break;
                                }
                            }
                            if ("886".equals(stringBuffer.toString())){
                                System.out.println(ip+"退出群聊");
                                inputStream.close();
                                socket.close();
                                return;
                            }
                            System.out.println(ip+":"+stringBuffer);

                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            });


        }
    }

    private void fun3() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("图片服务器已开启....");
        while (true){
            Socket socket = serverSocket.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream inputStream = socket.getInputStream();
                        String newFilename = UUID.randomUUID().toString().replace("-", "");
                        FileOutputStream fileOutputStream = new FileOutputStream("demo21\\src\\" + newFilename + ".jpg");
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = inputStream.read(buf)) != -1 ){
                            fileOutputStream.write(buf,0,len);
                        }
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(("success!!!"+"demo21\\src\\" + newFilename + ".jpg").getBytes());

                        outputStream.close();
                        fileOutputStream.close();
                        inputStream.close();

                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }finally {

                        try {
                            socket.close();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }).start();
        }
    }
}
